|
1
|
|
|
/* global it describe before */ |
|
2
|
|
|
|
|
3
|
1 |
|
process.env.NODE_ENV = 'test'; |
|
4
|
|
|
|
|
5
|
|
|
//Require the dev-dependencies |
|
6
|
1 |
|
const chai = require('chai'); |
|
7
|
1 |
|
const chaiHttp = require('chai-http'); |
|
8
|
1 |
|
const HTMLParser = require('node-html-parser'); |
|
9
|
|
|
|
|
10
|
1 |
|
const server = require('../../app.js'); |
|
11
|
|
|
|
|
12
|
1 |
|
chai.should(); |
|
13
|
|
|
|
|
14
|
1 |
|
const db = require("../db/database.js"); |
|
15
|
|
|
|
|
16
|
1 |
|
chai.use(chaiHttp); |
|
17
|
|
|
|
|
18
|
1 |
|
let apiKey = ""; |
|
19
|
|
|
|
|
20
|
1 |
|
describe('auth', () => { |
|
21
|
1 |
|
before(() => { |
|
22
|
1 |
|
db.run("DELETE FROM apiKeys", (err) => { |
|
23
|
2 |
|
if (err) { |
|
24
|
|
|
console.error("Could not empty test DB apiKeys", err.message); |
|
25
|
|
|
} |
|
26
|
|
|
}); |
|
27
|
|
|
|
|
28
|
1 |
|
db.run("DELETE FROM users", (err) => { |
|
29
|
2 |
|
if (err) { |
|
30
|
|
|
console.error("Could not empty test DB users", err.message); |
|
31
|
|
|
} |
|
32
|
|
|
}); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
1 |
|
describe('GET /api_key', () => { |
|
36
|
1 |
|
it('200 HAPPY PATH getting form', (done) => { |
|
37
|
1 |
|
chai.request(server) |
|
38
|
|
|
.get("/v2/auth/api_key") |
|
39
|
|
|
.end((err, res) => { |
|
40
|
1 |
|
res.should.have.status(200); |
|
41
|
|
|
|
|
42
|
1 |
|
done(); |
|
43
|
|
|
}); |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
1 |
|
it('should get 200 as we get apiKey', (done) => { |
|
47
|
1 |
|
let user = { |
|
48
|
|
|
email: "[email protected]", |
|
49
|
|
|
gdpr: "gdpr" |
|
50
|
|
|
}; |
|
51
|
|
|
|
|
52
|
1 |
|
chai.request(server) |
|
53
|
|
|
.post("/v2/auth/api_key/confirmation") |
|
54
|
|
|
.send(user) |
|
55
|
|
|
.end((err, res) => { |
|
56
|
1 |
|
res.should.have.status(200); |
|
57
|
1 |
|
res.text.should.be.a("string"); |
|
58
|
|
|
|
|
59
|
1 |
|
let HTMLResponse = HTMLParser.parse(res.text); |
|
60
|
1 |
|
let apiKeyElement = HTMLResponse.querySelector('#apikey'); |
|
61
|
|
|
|
|
62
|
1 |
|
apiKeyElement.should.be.an("object"); |
|
63
|
|
|
|
|
64
|
1 |
|
apiKey = apiKeyElement.childNodes[0].rawText; |
|
65
|
|
|
|
|
66
|
1 |
|
apiKey.length.should.equal(32); |
|
67
|
|
|
|
|
68
|
1 |
|
done(); |
|
69
|
|
|
}); |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
1 |
|
it('should get 200 but no apikey element not a valid email', (done) => { |
|
73
|
1 |
|
let user = { |
|
74
|
|
|
email: "test", |
|
75
|
|
|
gdpr: "gdpr" |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
1 |
|
chai.request(server) |
|
79
|
|
|
.post("/v2/auth/api_key/confirmation") |
|
80
|
|
|
.send(user) |
|
81
|
|
|
.end((err, res) => { |
|
82
|
1 |
|
res.should.have.status(200); |
|
83
|
1 |
|
res.text.should.be.a("string"); |
|
84
|
|
|
|
|
85
|
1 |
|
let HTMLResponse = HTMLParser.parse(res.text); |
|
86
|
1 |
|
let apiKeyElement = HTMLResponse.querySelector('#apikey'); |
|
87
|
|
|
|
|
88
|
1 |
|
(apiKeyElement === null).should.be.true; |
|
89
|
|
|
|
|
90
|
1 |
|
let messageElement = HTMLResponse.querySelector('#error'); |
|
91
|
|
|
|
|
92
|
1 |
|
messageElement.should.be.an("object"); |
|
93
|
|
|
|
|
94
|
1 |
|
let message = messageElement.childNodes[0].rawText; |
|
95
|
|
|
|
|
96
|
1 |
|
message.should.equal("A valid email address is required to obtain an API key."); |
|
97
|
|
|
|
|
98
|
1 |
|
done(); |
|
99
|
|
|
}); |
|
100
|
|
|
}); |
|
101
|
|
|
|
|
102
|
1 |
|
it('should get 200 but no apikey element no gdpr', (done) => { |
|
103
|
1 |
|
let user = { |
|
104
|
|
|
email: "[email protected]" |
|
105
|
|
|
}; |
|
106
|
|
|
|
|
107
|
1 |
|
chai.request(server) |
|
108
|
|
|
.post("/v2/auth/api_key/confirmation") |
|
109
|
|
|
.send(user) |
|
110
|
|
|
.end((err, res) => { |
|
111
|
1 |
|
res.should.have.status(200); |
|
112
|
1 |
|
res.text.should.be.a("string"); |
|
113
|
|
|
|
|
114
|
1 |
|
let HTMLResponse = HTMLParser.parse(res.text); |
|
115
|
1 |
|
let apiKeyElement = HTMLResponse.querySelector('#apikey'); |
|
116
|
|
|
|
|
117
|
1 |
|
(apiKeyElement === null).should.be.true; |
|
118
|
|
|
|
|
119
|
1 |
|
let messageElement = HTMLResponse.querySelector('#error'); |
|
120
|
|
|
|
|
121
|
1 |
|
messageElement.should.be.an("object"); |
|
122
|
|
|
|
|
123
|
1 |
|
let message = messageElement.childNodes[0].rawText; |
|
124
|
|
|
|
|
125
|
1 |
|
message.should.equal("Approve the terms and conditions."); |
|
126
|
|
|
|
|
127
|
1 |
|
done(); |
|
128
|
|
|
}); |
|
129
|
|
|
}); |
|
130
|
|
|
|
|
131
|
1 |
|
it('should get 200 but no apikey element not correct gdpr', (done) => { |
|
132
|
1 |
|
let user = { |
|
133
|
|
|
email: "[email protected]", |
|
134
|
|
|
gdpr: "gdprgdpr" |
|
135
|
|
|
}; |
|
136
|
|
|
|
|
137
|
1 |
|
chai.request(server) |
|
138
|
|
|
.post("/v2/auth/api_key/confirmation") |
|
139
|
|
|
.send(user) |
|
140
|
|
|
.end((err, res) => { |
|
141
|
1 |
|
res.should.have.status(200); |
|
142
|
1 |
|
res.text.should.be.a("string"); |
|
143
|
|
|
|
|
144
|
1 |
|
let HTMLResponse = HTMLParser.parse(res.text); |
|
145
|
1 |
|
let apiKeyElement = HTMLResponse.querySelector('#apikey'); |
|
146
|
|
|
|
|
147
|
1 |
|
(apiKeyElement === null).should.be.true; |
|
148
|
|
|
|
|
149
|
1 |
|
let messageElement = HTMLResponse.querySelector('#error'); |
|
150
|
|
|
|
|
151
|
1 |
|
messageElement.should.be.an("object"); |
|
152
|
|
|
|
|
153
|
1 |
|
let message = messageElement.childNodes[0].rawText; |
|
154
|
|
|
|
|
155
|
1 |
|
message.should.equal("Approve the terms and conditions."); |
|
156
|
|
|
|
|
157
|
1 |
|
done(); |
|
158
|
|
|
}); |
|
159
|
|
|
}); |
|
160
|
|
|
}); |
|
161
|
|
|
|
|
162
|
1 |
|
describe('POST /register', () => { |
|
163
|
1 |
|
it('should get 401 as we do not provide valid api_key', (done) => { |
|
164
|
1 |
|
let user = { |
|
165
|
|
|
email: "[email protected]", |
|
166
|
|
|
password: "123test", |
|
167
|
|
|
// api_key: apiKey |
|
168
|
|
|
}; |
|
169
|
|
|
|
|
170
|
1 |
|
chai.request(server) |
|
171
|
|
|
.post("/v2/auth/register") |
|
172
|
|
|
.send(user) |
|
173
|
|
|
.end((err, res) => { |
|
174
|
1 |
|
res.should.have.status(401); |
|
175
|
1 |
|
res.body.should.be.an("object"); |
|
176
|
1 |
|
res.body.errors.status.should.be.equal(401); |
|
177
|
1 |
|
done(); |
|
178
|
|
|
}); |
|
179
|
|
|
}); |
|
180
|
|
|
|
|
181
|
1 |
|
it('should get 401 as we do not provide email', (done) => { |
|
182
|
1 |
|
let user = { |
|
183
|
|
|
//email: "[email protected]", |
|
184
|
|
|
password: "123test", |
|
185
|
|
|
api_key: apiKey |
|
186
|
|
|
}; |
|
187
|
|
|
|
|
188
|
1 |
|
chai.request(server) |
|
189
|
|
|
.post("/v2/auth/register") |
|
190
|
|
|
.send(user) |
|
191
|
|
|
.end((err, res) => { |
|
192
|
1 |
|
res.should.have.status(401); |
|
193
|
1 |
|
res.body.should.be.an("object"); |
|
194
|
1 |
|
res.body.errors.status.should.be.equal(401); |
|
195
|
1 |
|
done(); |
|
196
|
|
|
}); |
|
197
|
|
|
}); |
|
198
|
|
|
|
|
199
|
1 |
|
it('should get 401 as we do not provide password', (done) => { |
|
200
|
1 |
|
let user = { |
|
201
|
|
|
email: "[email protected]", |
|
202
|
|
|
// password: "123test", |
|
203
|
|
|
api_key: apiKey |
|
204
|
|
|
}; |
|
205
|
|
|
|
|
206
|
1 |
|
chai.request(server) |
|
207
|
|
|
.post("/v2/auth/register") |
|
208
|
|
|
.send(user) |
|
209
|
|
|
.end((err, res) => { |
|
210
|
1 |
|
res.should.have.status(401); |
|
211
|
1 |
|
res.body.should.be.an("object"); |
|
212
|
1 |
|
res.body.errors.status.should.be.equal(401); |
|
213
|
1 |
|
done(); |
|
214
|
|
|
}); |
|
215
|
|
|
}); |
|
216
|
|
|
|
|
217
|
1 |
|
it('should get 201 HAPPY PATH', (done) => { |
|
218
|
1 |
|
let user = { |
|
219
|
|
|
email: "[email protected]", |
|
220
|
|
|
password: "123test", |
|
221
|
|
|
api_key: apiKey |
|
222
|
|
|
}; |
|
223
|
|
|
|
|
224
|
1 |
|
chai.request(server) |
|
225
|
|
|
.post("/v2/auth/register") |
|
226
|
|
|
.send(user) |
|
227
|
|
|
.end((err, res) => { |
|
228
|
1 |
|
res.should.have.status(201); |
|
229
|
1 |
|
res.body.should.be.an("object"); |
|
230
|
1 |
|
res.body.should.have.property("data"); |
|
231
|
1 |
|
res.body.data.should.have.property("message"); |
|
232
|
1 |
|
res.body.data.message.should.equal("User successfully registered."); |
|
233
|
|
|
|
|
234
|
1 |
|
done(); |
|
235
|
|
|
}); |
|
236
|
|
|
}); |
|
237
|
|
|
}); |
|
238
|
|
|
|
|
239
|
1 |
|
describe('POST /login', () => { |
|
240
|
1 |
|
it('should get 401 as we do not provide valid api_key', (done) => { |
|
241
|
1 |
|
let user = { |
|
242
|
|
|
email: "[email protected]", |
|
243
|
|
|
password: "123test", |
|
244
|
|
|
// api_key: apiKey |
|
245
|
|
|
}; |
|
246
|
|
|
|
|
247
|
1 |
|
chai.request(server) |
|
248
|
|
|
.post("/v2/auth/login") |
|
249
|
|
|
.send(user) |
|
250
|
|
|
.end((err, res) => { |
|
251
|
1 |
|
res.should.have.status(401); |
|
252
|
1 |
|
res.body.should.be.an("object"); |
|
253
|
1 |
|
res.body.errors.status.should.be.equal(401); |
|
254
|
1 |
|
done(); |
|
255
|
|
|
}); |
|
256
|
|
|
}); |
|
257
|
|
|
|
|
258
|
1 |
|
it('should get 401 as we do not provide email', (done) => { |
|
259
|
1 |
|
let user = { |
|
260
|
|
|
//email: "[email protected]", |
|
261
|
|
|
password: "123test", |
|
262
|
|
|
api_key: apiKey |
|
263
|
|
|
}; |
|
264
|
|
|
|
|
265
|
1 |
|
chai.request(server) |
|
266
|
|
|
.post("/v2/auth/login") |
|
267
|
|
|
.send(user) |
|
268
|
|
|
.end((err, res) => { |
|
269
|
1 |
|
res.should.have.status(401); |
|
270
|
1 |
|
res.body.should.be.an("object"); |
|
271
|
1 |
|
res.body.errors.status.should.be.equal(401); |
|
272
|
1 |
|
done(); |
|
273
|
|
|
}); |
|
274
|
|
|
}); |
|
275
|
|
|
|
|
276
|
1 |
|
it('should get 401 as we do not provide password', (done) => { |
|
277
|
1 |
|
let user = { |
|
278
|
|
|
email: "[email protected]", |
|
279
|
|
|
// password: "123test", |
|
280
|
|
|
api_key: apiKey |
|
281
|
|
|
}; |
|
282
|
|
|
|
|
283
|
1 |
|
chai.request(server) |
|
284
|
|
|
.post("/v2/auth/login") |
|
285
|
|
|
.send(user) |
|
286
|
|
|
.end((err, res) => { |
|
287
|
1 |
|
res.should.have.status(401); |
|
288
|
1 |
|
res.body.should.be.an("object"); |
|
289
|
1 |
|
res.body.errors.status.should.be.equal(401); |
|
290
|
1 |
|
done(); |
|
291
|
|
|
}); |
|
292
|
|
|
}); |
|
293
|
|
|
|
|
294
|
1 |
|
it('should get 401 as user not found', (done) => { |
|
295
|
1 |
|
let user = { |
|
296
|
|
|
email: "[email protected]", |
|
297
|
|
|
password: "123test", |
|
298
|
|
|
api_key: apiKey |
|
299
|
|
|
}; |
|
300
|
|
|
|
|
301
|
1 |
|
chai.request(server) |
|
302
|
|
|
.post("/v2/auth/login") |
|
303
|
|
|
.send(user) |
|
304
|
|
|
.end((err, res) => { |
|
305
|
1 |
|
res.should.have.status(401); |
|
306
|
1 |
|
res.body.should.be.an("object"); |
|
307
|
1 |
|
res.body.errors.status.should.be.equal(401); |
|
308
|
1 |
|
done(); |
|
309
|
|
|
}); |
|
310
|
|
|
}); |
|
311
|
|
|
|
|
312
|
1 |
|
it('should get 401 incorrect password', (done) => { |
|
313
|
1 |
|
let user = { |
|
314
|
|
|
email: "[email protected]", |
|
315
|
|
|
password: "wrongpassword", |
|
316
|
|
|
api_key: apiKey |
|
317
|
|
|
}; |
|
318
|
|
|
|
|
319
|
1 |
|
chai.request(server) |
|
320
|
|
|
.post("/v2/auth/login") |
|
321
|
|
|
.send(user) |
|
322
|
|
|
.end((err, res) => { |
|
323
|
1 |
|
res.should.have.status(401); |
|
324
|
1 |
|
res.body.should.be.an("object"); |
|
325
|
1 |
|
res.body.errors.status.should.be.equal(401); |
|
326
|
1 |
|
done(); |
|
327
|
|
|
}); |
|
328
|
|
|
}); |
|
329
|
|
|
|
|
330
|
1 |
|
it('should get 201 HAPPY PATH', (done) => { |
|
331
|
1 |
|
let user = { |
|
332
|
|
|
email: "[email protected]", |
|
333
|
|
|
password: "123test", |
|
334
|
|
|
api_key: apiKey |
|
335
|
|
|
}; |
|
336
|
|
|
|
|
337
|
1 |
|
chai.request(server) |
|
338
|
|
|
.post("/v2/auth/login") |
|
339
|
|
|
.send(user) |
|
340
|
|
|
.end((err, res) => { |
|
341
|
1 |
|
res.should.have.status(200); |
|
342
|
1 |
|
res.body.should.be.an("object"); |
|
343
|
1 |
|
res.body.should.have.property("data"); |
|
344
|
1 |
|
res.body.data.should.have.property("type"); |
|
345
|
1 |
|
res.body.data.type.should.equal("success"); |
|
346
|
1 |
|
res.body.data.should.have.property("type"); |
|
347
|
|
|
|
|
348
|
1 |
|
done(); |
|
349
|
|
|
}); |
|
350
|
|
|
}); |
|
351
|
|
|
}); |
|
352
|
|
|
}); |
|
353
|
|
|
|